<?php
namespace app\api\model;

use think\Model;

class Users extends Base{
    protected $name = 'users';
    protected $updateTime = false;

    /**
     * 生成用户唯一识别码
     * @param int $id 用户id
     * @return string
     */
    public function MakeUserOnly($id)
    {
        $sn  = date('Y', time());
        $sn  = substr($sn, -2);
        $sn .= date('m', time()) . $id;
        $sn .= rand(11, 99);
        return $sn;
    }

    // 获取用户信息
    public function getUserInfo($where){
        $info = $this->getFind($where);

        if($info['code'] != 'ok'){
            return '该用户不存在';
        }

        $field = 'money, point, card_money, money_gold, star_id, is_buy, status, create_time, star_time, gold_time';
        $userShop = (new UserShop())->getFind(['user_id' => $info['data']['id'], 'admin_id' => $info['data']['bind_admin_id']], $field);

        if($userShop['code'] != 'ok'){
            return $info['data'];
        }

        if($userShop['data']['star_id']){
            $star_info = (new Star())->where(['id' => $userShop['data']['star_id']])->find();
            $userShop['data']['star_discount'] = $star_info['discount'];
        }else{
            $userShop['data']['star_discount'] = 1;
        }

        $info['data']['user_shop'] = $userShop['data'];

        return $info['data'];
    }

    /** 查询用户下两级的所有人 */
    public function getUserPush($user_id){
        $user_fst_push_ids = $this->where(['pid' => $user_id])->column('id');
        $user_sec_push_ids = $this->where([['id', 'in', $user_fst_push_ids]])->column('id');

        $user_push_ids = array_merge($user_fst_push_ids, $user_sec_push_ids);

        return $user_push_ids;
    }
}